//
//  KATStandardTableHeader.m
//  KATFramework
//
//  Created by Kantice on 16/7/15.
//  Copyright © 2016年 KatApp. All rights reserved.
//

#import "KATStandardTableHeader.h"

@implementation KATStandardTableHeader

#pragma -mark 类方法

//获取实例
+ (instancetype)tableHeaderWithHeight:(float)height andScrollView:(UIScrollView *)scrollView
{
    if(!scrollView)
    {
        return nil;
    }
    
    KATStandardTableHeader *header=[[[self alloc] init] autorelease];
    
    [header initWithHeight:height andScrollView:scrollView];
    header.stateDelegate=header;
    
    header.hasTite=YES;
    header.hasTime=YES;
    header.isUpdateTime=YES;
    header.hasPullingIndicator=YES;
    header.hasLoadingIndicator=YES;
    header.hasSuccessIndicator=YES;
    header.hasFailIndicator=YES;
    
    header.infoPulling=@"pull to load!";
    header.infoRelease=@"release to load!";
    header.infoLoading=@"loading...";
    header.infoSuccess=@"load success!";
    header.infoFail=@"load fail!";
    header.infoTime=@"update time: ";
    
    header.animationPulling=ST_HEADER_ANIM_STROKE_END;
    header.animationLoading=ST_HEADER_ANIM_ROTATE;
    header.animationPullingDuration=0.24;
    header.animationLoadingDuration=2.0;
    header.animationPullingRotateStartValue=0.0;
    header.animationPullingRotateEndValue=0.5;
    header.animationPullingScaleXStartValue=0.0;
    header.animationPullingScaleXEndValue=1.0;
    header.animationPullingScaleYStartValue=0.0;
    header.animationPullingScaleYEndValue=1.0;
    header.animationPullingStrokeStartStartValue=1.0;
    header.animationPullingStrokeStartEndValue=0.0;
    header.animationPullingStrokeEndStartValue=0.0;
    header.animationPullingStrokeEndEndValue=1.0;
    header.animationPullingAlphaStartValue=0.0;
    header.animationPullingAlphaEndValue=1.0;
    header.animationLoadingRotateStartValue=0.0;
    header.animationLoadingRotateEndValue=1.0;
    header.animationLoadingScaleXStartValue=0.0;
    header.animationLoadingScaleXEndValue=1.0;
    header.animationLoadingScaleYStartValue=0.0;
    header.animationLoadingScaleYEndValue=1.0;
    header.animationLoadingStrokeStartStartValue=1.0;
    header.animationLoadingStrokeStartEndValue=0.0;
    header.animationLoadingStrokeEndStartValue=0.0;
    header.animationLoadingStrokeEndEndValue=1.0;
    header.animationLoadingAlphaStartValue=0.0;
    header.animationLoadingAlphaEndValue=1.0;
    header.isAnimationLoadingAutoReverse=NO;
    header.isAnimationPullingActAtRelease=NO;
    
    header.dateFormatter=[[[NSDateFormatter alloc] init] autorelease];
    [header.dateFormatter setDateFormat:@"YYYY/MM/dd HH:mm:ss"];
    header.updateTime=nil;
    
    header.indicatorTypePulling=SHAPE_DAISY;
    header.indicatorTypeLoading=SHAPE_DAISY;
    header.indicatorTypeSuccess=SHAPE_SUCCESS;
    header.indicatorTypeFail=SHAPE_FAIL;
    
    return header;
}


#pragma -mark 对象方法


//初始化
- (void)initHeader
{
    //标题
    self.title=[[[UILabel alloc] init] autorelease];
    _title.frame=CGRectMake(self.bounds.size.height*1.0, self.bounds.size.height*0.2, self.bounds.size.width-self.bounds.size.height*2.0, self.bounds.size.height*0.5);
    _title.text=_infoPulling;
    _title.textColor=KCOLOR_DARK;
    _title.textAlignment=NSTextAlignmentCenter;
    _title.numberOfLines=0;
    _title.font=[UIFont systemFontOfSize:self.bounds.size.height*0.28];
    _title.backgroundColor=[UIColor clearColor];
    [self addSubview:_title];
    
    //刷新时间
    self.time=[[[UILabel alloc] init] autorelease];
    _time.frame=CGRectMake(self.bounds.size.height*1.0, self.bounds.size.height*0.65, self.bounds.size.width-self.bounds.size.height*2.0, self.bounds.size.height*0.25);
    _time.text=@"";
    _time.textColor=KCOLOR_GRAY;
    _time.textAlignment=NSTextAlignmentCenter;
    _time.numberOfLines=0;
    _time.font=[UIFont systemFontOfSize:self.bounds.size.height*0.2];
    _time.backgroundColor=[UIColor clearColor];
    [self addSubview:_time];
    
    if(!_isUpdateTime)//不刷新时间，则一直显示提示
    {
        _time.text=_infoTime;
    }
    
    
    //指示器
    
    if(![KATShapeUtil hasShapeType:_indicatorTypePulling])
    {
        _indicatorTypePulling=SHAPE_DAISY;
    }

    self.indicatorPulling=[KATShapeUtil shapeWithType:_indicatorTypePulling andWidth:self.bounds.size.height*0.5 andHeight:self.bounds.size.height*0.5];
    _indicatorPulling.lineWidth=1.0;
    _indicatorPulling.strokeColor=KCOLOR_BLUE.CGColor;
    _indicatorPulling.animationTimingMode=kCAMediaTimingFunctionLinear;
    _indicatorPulling.position=CGPointMake(self.bounds.size.height*0.5, self.bounds.size.height*0.5);
    [self.layer addSublayer:_indicatorPulling];
    
    
    if(![KATShapeUtil hasShapeType:_indicatorTypeLoading])
    {
        _indicatorTypeLoading=SHAPE_DAISY;
    }
    
    self.indicatorLoading=[KATShapeUtil shapeWithType:_indicatorTypeLoading andWidth:self.bounds.size.height*0.5 andHeight:self.bounds.size.height*0.5];
    _indicatorLoading.lineWidth=1.0;
    _indicatorLoading.strokeColor=KCOLOR_BLUE.CGColor;
    _indicatorLoading.animationTimingMode=kCAMediaTimingFunctionLinear;
    _indicatorLoading.position=CGPointMake(self.bounds.size.height*0.5, self.bounds.size.height*0.5);
    [self.layer addSublayer:_indicatorLoading];
    
    
    if(![KATShapeUtil hasShapeType:_indicatorTypeSuccess])
    {
        _indicatorTypeSuccess=SHAPE_SUCCESS;
    }
    
    self.indicatorSuccess=[KATShapeUtil shapeWithType:_indicatorTypeSuccess andWidth:self.bounds.size.height*0.5 andHeight:self.bounds.size.height*0.5];
    _indicatorSuccess.lineWidth=1.0;
    _indicatorSuccess.strokeColor=KCOLOR_BLUE.CGColor;
    _indicatorSuccess.position=CGPointMake(self.bounds.size.height*0.5, self.bounds.size.height*0.5);
    [self.layer addSublayer:_indicatorSuccess];
    
    
    if(![KATShapeUtil hasShapeType:_indicatorTypeFail])
    {
        _indicatorTypeFail=SHAPE_FAIL;
    }
    
    self.indicatorFail=[KATShapeUtil shapeWithType:_indicatorTypeFail andWidth:self.bounds.size.height*0.5 andHeight:self.bounds.size.height*0.5];
    _indicatorFail.lineWidth=1.0;
    _indicatorFail.strokeColor=KCOLOR_PINK.CGColor;
    _indicatorFail.position=CGPointMake(self.bounds.size.height*0.5, self.bounds.size.height*0.5);
    [self.layer addSublayer:_indicatorFail];
    
    
    //提示
    self.tips=[KATToast toast];
    _tips.position=TOAST_POSITION_BOTTOM;
    _tips.duration=TOAST_DURATION_SHORT;
    _tips.canTap=YES;
    
    
    //指示器状态初始化
    [self restoreIndicators];
    
}


//加载成功
- (void)successWithMessage:(NSString *)message
{
    [self restoreIndicators];
    
    if(_hasSuccessIndicator)
    {
        _indicatorSuccess.hidden=NO;
    }
    
    _title.text=_infoSuccess;
    
    if(message)//显示toast
    {
        _tips.text=message;
        [_tips show];
    }
    
    //更新时间
    if(_isUpdateTime)
    {
        self.updateTime=[NSDate date];
        _time.text=[NSString stringWithFormat:@"%@%@",_infoTime,[_dateFormatter stringFromDate:_updateTime]];
    }
    
    [self finishLoad];
}


//加载失败
- (void)failWithMessage:(NSString *)message
{
    [self restoreIndicators];
    
    if(_hasFailIndicator)
    {
        _indicatorFail.hidden=NO;
    }
    
    _title.text=_infoFail;
    
    if(message)//显示toast
    {
        _tips.text=message;
        [_tips show];
    }
    
    [self finishLoad];
}


//内部函数：恢复指示器
- (void)restoreIndicators
{
    [_indicatorPulling clearAnimations];
    [_indicatorPulling restoreShape];
    [_indicatorLoading clearAnimations];
    [_indicatorLoading restoreShape];
    
    _indicatorPulling.hidden=YES;
    _indicatorLoading.hidden=YES;
    _indicatorFail.hidden=YES;
    _indicatorSuccess.hidden=YES;
    
    if(!_hasTite)
    {
        _title.hidden=YES;
    }
    else
    {
        _title.hidden=NO;
    }
    
    if(!_hasTime)
    {
        _time.hidden=YES;
    }
    else
    {
        _time.hidden=NO;
    }
    
    
    //下拉动画
    if(_animationPulling/ST_HEADER_ANIM_ROTATE%10>0)//旋转动画
    {
        [_indicatorPulling transformRotateToAngle:_animationPullingRotateStartValue*M_PI*2];
    }
    
    if(_animationPulling/ST_HEADER_ANIM_SCALE_X%10>0 || _animationPulling/ST_HEADER_ANIM_SCALE_Y%10>0)//缩放动画
    {
        float w=(_animationPulling/ST_HEADER_ANIM_SCALE_X%10>0?_animationPullingScaleXStartValue:1.0);
        float h=(_animationPulling/ST_HEADER_ANIM_SCALE_Y%10>0?_animationPullingScaleYStartValue:1.0);
        
        [_indicatorPulling transformScaleToWitdh:w andHeight:h];
    }
    
    if(_animationPulling/ST_HEADER_ANIM_STROKE_START%10>0)//初始点动画
    {
        _indicatorPulling.strokeStart=_animationPullingStrokeStartStartValue;
    }
    
    if(_animationPulling/ST_HEADER_ANIM_STROKE_END%10>0)//结束点动画
    {
        _indicatorPulling.strokeEnd=_animationPullingStrokeEndStartValue;
    }
    
    if(_animationPulling/ST_HEADER_ANIM_ALPHA%10>0)//透明度动画
    {
        _indicatorPulling.opacity=_animationPullingAlphaStartValue;
    }
    
    //加载动画
    if(_animationLoading/ST_HEADER_ANIM_ROTATE%10>0)//旋转动画
    {
        [_indicatorLoading transformRotateToAngle:_animationLoadingRotateStartValue*M_PI*2];
    }
    
    if(_animationLoading/ST_HEADER_ANIM_SCALE_X%10>0 || _animationLoading/ST_HEADER_ANIM_SCALE_Y%10>0)//缩放动画
    {
        float w=(_animationLoading/ST_HEADER_ANIM_SCALE_X%10>0?_animationLoadingRotateStartValue:1.0);
        float h=(_animationLoading/ST_HEADER_ANIM_SCALE_Y%10>0?_animationLoadingRotateStartValue:1.0);
        
        [_indicatorLoading transformScaleToWitdh:w andHeight:h];
    }
    
    if(_animationLoading/ST_HEADER_ANIM_STROKE_START%10>0)//初始点动画
    {
        _indicatorLoading.strokeStart=_animationLoadingStrokeStartStartValue;
    }
    
    if(_animationLoading/ST_HEADER_ANIM_STROKE_END%10>0)//结束点动画
    {
        _indicatorLoading.strokeEnd=_animationLoadingStrokeEndStartValue;
    }
    
    if(_animationLoading/ST_HEADER_ANIM_ALPHA%10>0)//透明度动画
    {
        _indicatorLoading.opacity=_animationLoadingAlphaStartValue;
    }
    
//    [_tips hide];
}


#pragma -mark 回调方法

//下拉
- (void)headerPullWithProgress:(float)progress
{
    if(_hasPullingIndicator && self.state!=THEADER_STATE_LOADING && self.state!=THEADER_STATE_FINISH)
    {
        _indicatorPulling.hidden=NO;
    }
    
    //更改标题内容
    if(progress>=1.0)
    {
        if(self.state!=THEADER_STATE_RELEASE && self.state!=THEADER_STATE_LOADING && self.state!=THEADER_STATE_FINISH)
        {
            _title.text=_infoRelease;
        }
    }
    else
    {
        if(self.state!=THEADER_STATE_PULLING && self.state!=THEADER_STATE_LOADING && self.state!=THEADER_STATE_FINISH)
        {
            _title.text=_infoPulling;
        }
    }
    
    //下拉动画
    if(_isAnimationPullingActAtRelease)//下拉到底时执行动画
    {
        if(progress>=1.0)//下拉到底
        {
            if(self.state==THEADER_STATE_PULLING)
            {
                [_indicatorPulling clearAnimations];
                
                NSMutableArray *animations=[NSMutableArray arrayWithCapacity:10];
                
                if(_animationPulling/ST_HEADER_ANIM_ROTATE%10>0)//旋转动画
                {
                    [animations addObject:[_indicatorPulling animationRotationZToAngle:_animationPullingRotateEndValue*M_PI*2 withDuration:_animationPullingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPulling/ST_HEADER_ANIM_SCALE_X%10>0)//X轴缩放动画
                {
                    [animations addObject:[_indicatorPulling animationScaleXToSize:_animationPullingScaleXEndValue withDuration:_animationPullingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPulling/ST_HEADER_ANIM_SCALE_Y%10>0)//Y轴缩放动画
                {
                    [animations addObject:[_indicatorPulling animationScaleYToSize:_animationPullingScaleYEndValue withDuration:_animationPullingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPulling/ST_HEADER_ANIM_STROKE_START%10>0)//初始点动画
                {
                    [animations addObject:[_indicatorPulling animationStrokeStartToValue:_animationPullingStrokeStartEndValue withDuration:_animationPullingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPulling/ST_HEADER_ANIM_STROKE_END%10>0)//结束点动画
                {
                    [animations addObject:[_indicatorPulling animationStrokeEndToValue:_animationPullingStrokeEndEndValue withDuration:_animationPullingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPulling/ST_HEADER_ANIM_ALPHA%10>0)//透明度动画
                {
                    [animations addObject:[_indicatorPulling animationToOpacity:_animationPullingAlphaEndValue withDuration:_animationPullingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                [_indicatorPulling startAnimation:[_indicatorPulling animationWithGroup:animations duration:_animationPullingDuration repeat:1 autoreverses:NO delay:0] andDelay:0];
            }
        }
        else//未下拉到底
        {
            if(self.state==THEADER_STATE_RELEASE)
            {
                [_indicatorPulling clearAnimations];
                
                NSMutableArray *animations=[NSMutableArray arrayWithCapacity:10];
                
                if(_animationPulling/ST_HEADER_ANIM_ROTATE%10>0)//旋转动画
                {
                    [animations addObject:[_indicatorPulling animationRotationZToAngle:_animationPullingRotateStartValue*M_PI*2 withDuration:_animationPullingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPulling/ST_HEADER_ANIM_SCALE_X%10>0)//X轴缩放动画
                {
                    [animations addObject:[_indicatorPulling animationScaleXToSize:_animationPullingScaleXStartValue withDuration:_animationPullingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPulling/ST_HEADER_ANIM_SCALE_Y%10>0)//Y轴缩放动画
                {
                    [animations addObject:[_indicatorPulling animationScaleYToSize:_animationPullingScaleYStartValue withDuration:_animationPullingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPulling/ST_HEADER_ANIM_STROKE_START%10>0)//初始点动画
                {
                    [animations addObject:[_indicatorPulling animationStrokeStartToValue:_animationPullingStrokeStartStartValue withDuration:_animationPullingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPulling/ST_HEADER_ANIM_STROKE_END%10>0)//结束点动画
                {
                    [animations addObject:[_indicatorPulling animationStrokeEndToValue:_animationPullingStrokeEndStartValue withDuration:_animationPullingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPulling/ST_HEADER_ANIM_ALPHA%10>0)//透明度动画
                {
                    [animations addObject:[_indicatorPulling animationToOpacity:_animationPullingAlphaStartValue withDuration:_animationPullingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                [_indicatorPulling startAnimation:[_indicatorPulling animationWithGroup:animations duration:_animationPullingDuration repeat:1 autoreverses:NO delay:0] andDelay:0];
            }
        }
    }
    else
    {
        if(_animationPulling/ST_HEADER_ANIM_ROTATE%10>0)//旋转动画
        {
            [_indicatorPulling transformRotateToAngle:_animationPullingRotateStartValue*M_PI*2+progress*(_animationPullingRotateEndValue-_animationPullingRotateStartValue)*M_PI*2];
        }
        
        if(_animationPulling/ST_HEADER_ANIM_SCALE_X%10>0 || _animationPulling/ST_HEADER_ANIM_SCALE_Y%10>0)//缩放动画
        {
            float w=(_animationPulling/ST_HEADER_ANIM_SCALE_X%10>0?_animationPullingScaleXStartValue+progress*(_animationPullingScaleXEndValue-_animationPullingScaleXStartValue):1.0);
            float h=(_animationPulling/ST_HEADER_ANIM_SCALE_Y%10>0?_animationPullingScaleYStartValue+progress*(_animationPullingScaleYEndValue-_animationPullingScaleYStartValue):1.0);
            
            [_indicatorPulling transformScaleToWitdh:w andHeight:h];
        }
        
        if(_animationPulling/ST_HEADER_ANIM_STROKE_START%10>0)//初始点动画
        {
            _indicatorPulling.strokeStart=progress;
        }
        
        if(_animationPulling/ST_HEADER_ANIM_STROKE_END%10>0)//结束点动画
        {
            _indicatorPulling.strokeEnd=progress;
        }
        
        if(_animationPulling/ST_HEADER_ANIM_ALPHA%10>0)//透明度动画
        {
            _indicatorPulling.opacity=progress;
        }
    }
}


//下拉完成
- (void)headerFinishPull
{    
    [self headerPullWithProgress:1.0f];
}


//开始加载
- (void)headerStartLoad
{
    self.state=THEADER_STATE_LOADING;
    
    [self restoreIndicators];
    
    if(_hasLoadingIndicator)
    {
        _indicatorLoading.hidden=NO;
    }
    
    _title.text=_infoLoading;
    
    //动画
    NSMutableArray *animations=[NSMutableArray arrayWithCapacity:10];
    
    if(_animationLoading/ST_HEADER_ANIM_ROTATE%10>0)//旋转动画
    {
        [animations addObject:[_indicatorLoading animationRotationZToAngle:_animationLoadingRotateEndValue*M_PI*2 withDuration:_animationLoadingDuration repeat:1 autoreverses:NO delay:0]];
    }
    
    if(_animationLoading/ST_HEADER_ANIM_SCALE_X%10>0)//X轴缩放动画
    {
        [animations addObject:[_indicatorLoading animationScaleXToSize:_animationLoadingScaleXEndValue withDuration:_animationLoadingDuration repeat:1 autoreverses:NO delay:0]];
    }
    
    if(_animationLoading/ST_HEADER_ANIM_SCALE_Y%10>0)//Y轴缩放动画
    {
        [animations addObject:[_indicatorLoading animationScaleYToSize:_animationLoadingScaleYEndValue withDuration:_animationLoadingDuration repeat:1 autoreverses:NO delay:0]];
    }
    
    if(_animationLoading/ST_HEADER_ANIM_STROKE_START%10>0)//初始点动画
    {
        [animations addObject:[_indicatorLoading animationStrokeStartToValue:_animationLoadingStrokeStartEndValue withDuration:_animationLoadingDuration repeat:1 autoreverses:NO delay:0]];
    }
    
    if(_animationLoading/ST_HEADER_ANIM_STROKE_END%10>0)//结束点动画
    {
        [animations addObject:[_indicatorLoading animationStrokeEndToValue:_animationLoadingStrokeEndEndValue withDuration:_animationLoadingDuration repeat:1 autoreverses:NO delay:0]];
    }
    
    if(_animationLoading/ST_HEADER_ANIM_ALPHA%10>0)//透明度动画
    {
        [animations addObject:[_indicatorLoading animationToOpacity:_animationLoadingAlphaEndValue withDuration:_animationLoadingDuration repeat:1 autoreverses:NO delay:0]];
    }
    
    [_indicatorLoading startAnimation:[_indicatorLoading animationWithGroup:animations duration:_animationLoadingDuration repeat:-1 autoreverses:NO delay:0] andDelay:0];
}


//加载完成
- (void)headerFinishLoad
{
    
}


//加载完成后恢复
- (void)headerRestoreAfterLoad
{
    [self restoreIndicators];
    
    if(_hasPullingIndicator)
    {
        _indicatorPulling.hidden=NO;
    }
    
    _title.text=_infoPulling;
}


//释放内存
- (void)dealloc
{
    [_title release];
    [_time release];
    [_indicatorPulling release];
    [_indicatorLoading release];
    [_indicatorSuccess release];
    [_indicatorFail release];
    [_infoPulling release];
    [_infoRelease release];
    [_infoLoading release];
    [_infoSuccess release];
    [_infoFail release];
    [_infoTime release];
    [_tips release];
    [_dateFormatter release];
    [_updateTime release];
    
    [super dealloc];
}

@end
